<?php namespace App\Http\Controllers\Index;

use App\Services\ItemImageService;
use Auth;
use Illuminate\Contracts\Auth\Guard;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use App\Http\Requests\ItemRequest;
use App\Http\Requests\ItemImageRequest;
use App\Services\ItemService;
use App\Services\UserService;
use App\Models\Item;
use App\Models\Catalog;
use App\Models\Area;

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     */
    public function getIndex()
    {
        return view('index.user.index');
    }

    /**
     */
    public function getEdit()
    {
        return view('index.user.edit', ['user' => Auth::user()]);
    }

    /**
     */
    public function postEdit(UserService $userService, UserRequest $userRequest)
    {
        $userService->update(Auth::user()->id, [
            'name' => $userRequest->input('name'),
            'phone' => $userRequest->input('phone'),
            'gender' => $userRequest->input('gender', 'male'),
            'address' => $userRequest->input('address')
        ]);
        return redirect('user/edit')->with('message', '编辑资料成功');
    }

    public function getChangePassword()
    {
        return view('index.user.changePassword');
    }

    /**
     * TODO 修改密码流程未完成
     * @param UserService $userService
     * @param UserRequest $userRequest
     * @param Guard $auth
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postChangePassword(UserService $userService, UserRequest $userRequest, Guard $auth)
    {
        if ($auth->validate(['password' => $userRequest->input('old_password')])) {
            $userService->changePassword(Auth::user()->id, $userRequest->input('password'));
            return redirect('user/change-password')->with('message', '密码修改成功 ');
        }
        return redirect()->back()->withInput($userRequest->all())->withErrors(['message' => '密码错误',]);
    }

    public function getItems(ItemService $itemService, Request $request)
    {
        return view('index.user.items', ['items' => $itemService->getList($request, Auth::user()->id)]);
    }

    public function getItem($id)
    {
        return view('index.user.item', ['item' => Item::find($id)]);
    }

    public function getAddItem(Request $request)
    {
        if (!empty($request->old())) {
            $request->merge($request->old());
        }
        $request->merge($request->old());
        return view('index.user.addItem', [
            'request' => $request,
            'types' => Item::$_typeName,
            'provinces' => Area::where(['p_id' => 0, 'level' => 1])->get(),
            'cities' => Area::where(['p_id' => $request->input('province_id'), 'level' => 2])->get(),
            'districts' => Area::where(['p_id' => $request->input('city_id'), 'level' => 3])->get(),
            'p_catalogs' => Catalog::where(['p_id' => 0, 'level' => 1])->get(),
            'catalogs' => Catalog::where(['p_id' => $request->input('p_catalog_id'), 'level' => 2])->get(),
        ]);
    }

    public function postAddItem(ItemService $itemService, ItemRequest $itemRequest)
    {
        $itemService->create($itemRequest->all());
        return redirect('user/items')->with('message', '添加失物招领' . ($itemRequest->input('match_id') ? '并匹配成功' : '成功'));
    }


    public function getDeleteItem($id, ItemService $itemService)
    {
        $itemService->delete($id);
        return redirect('user/items')->with('message', '删除失物招领成功');
    }

    public function postAddItemImage(ItemImageService $itemImageService, ItemImageRequest $itemImageRequest)
    {
        $itemImageService->create($itemImageRequest);
        return response()->json(['message' => '上传成功']);
    }
}